<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2020-11-19 11:02:53
// +----------------------------------------------------------------------

namespace buwang\event;
use think\facade\Route;
use think\Response;

/**
 * 初始化配置信息
 * Class Miniapp
 * @package buwang\event
 */
class Miniapp
{
    public function handle()
    {
        //设置应用数据表前缀
        set_miniapp_database_prefix(app('http')->getName());

        //对所有应用有效的路由
        Route::get('app_front/:app_name/app', function ($app_name) {
            $app_path = app()->getRootPath() . 'public' . DS . 'app_front'. DS. $app_name. DS . 'index.html';
            if(file_exists($app_path)){
                return view(app()->getRootPath() . 'public' . DS . 'app_front'. DS. $app_name. DS . 'index.html');
            } else {
                return view(app()->getRootPath() . 'buwang' . DS . 'util'. DS. 'html'. DS . 'app_path_error.html',[
                    'app_name' => $app_name, // 传递路由参数
                ]);
            }
        });
        //初始化Addons配置信息
        $this->initAddonsConfig();
    }

    private function initAddonsConfig(): void
    {
        // 手动加载 helper.php
        require_once root_path('extend/think').'helper.php';
        $instance = new \think\addons\Service(app());
        $instance->register();

        if (method_exists($instance, 'boot')) {
            $instance->boot();
        }

    }


}
